package com.igeekhome.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.igeekhome.biz.*;
import com.igeekhome.pojo.CustomerService;
import com.igeekhome.pojo.SystemMessage;
import com.igeekhome.pojo.ViewStatistics;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.jar.JarEntry;
import java.io.File;
import java.io.IOException;


/**
 * <p>
 * 客服表 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2021-03-16
 */
@Controller
@RequestMapping("/customerService")
public class CustomerServiceController {

    @Autowired
    private ICustomerServiceService iCustomerServiceService;

    @Autowired
    private ICustomerInfoService iCustomerInfoService;

    @Autowired
    private IAttendanceStatisticsService iAttendanceStatisticsService;

    @Autowired
    private IWorkQualityStatisticsService iWorkQualityStatisticsService;

    @Autowired
    private IViewStatisticsService iViewStatisticsService;

    @Autowired
    private ISystemMessageService iSystemMessageService;

    @RequestMapping ("/login")
    public String checkCustomerServiceNameAndPwd(CustomerService customerService, Model model, HttpSession session)
    {


        String path="";
//        System.out.println(customerService);
        QueryWrapper qu=new QueryWrapper();
        qu.eq("email",customerService.getEmail());
        qu.eq("password",customerService.getPassword());
        CustomerService cs=iCustomerServiceService.getOne(qu);
        if(cs!=null){
            //找到了email
            session.setAttribute("cs",cs);


            QueryWrapper qw1 =new QueryWrapper();
            //工作质量
            int sumeffsess1=iWorkQualityStatisticsService.getSumeffsess(qw1);
            int sumresolve1=iWorkQualityStatisticsService.getSumresolve(qw1);
            int sumnosolve1=iWorkQualityStatisticsService.getSumunsolve(qw1);
            int sumgood1=iWorkQualityStatisticsService.getSumgood(qw1);
            int summedium1=iWorkQualityStatisticsService.getSummedium(qw1);
            int sumbad1=iWorkQualityStatisticsService.getSumbad(qw1);
            int sumno1=iWorkQualityStatisticsService.getSumno(qw1);

            session.setAttribute("sumeffsess1",sumeffsess1);
            session.setAttribute("sumresolve1",sumresolve1);
            session.setAttribute("sumnosolve1",sumnosolve1);
            session.setAttribute("sumgood1",sumgood1);
            session.setAttribute("summedium1",summedium1);
            session.setAttribute("sumbad1",sumbad1);
            session.setAttribute("sumno1",sumno1);
            //考勤统计
            QueryWrapper qw2 =new QueryWrapper();
            int maxLogin1=iAttendanceStatisticsService.getLogin(qw2);
            int maxFree1=iAttendanceStatisticsService.getFree(qw2);
            int maxBusy1=iAttendanceStatisticsService.getBusy(qw2);
            int maxOnline1=iAttendanceStatisticsService.getOnline(qw2);
            int maxOffline1=iAttendanceStatisticsService.getOffice(qw2);
            System.out.println(maxLogin1);
            //考勤model
            session.setAttribute("maxLogin1",maxLogin1);
            session.setAttribute("maxFree1",maxFree1);
            session.setAttribute("maxBusy1",maxBusy1);
            session.setAttribute("maxOnline1",maxOnline1);
            session.setAttribute("maxOffline1",maxOffline1);

            QueryWrapper qw10 =new QueryWrapper();
            qw10.eq("viewType","微信小程序");
            ViewStatistics weixin1=iViewStatisticsService.getOne(qw10);
            session.setAttribute("weixin1",weixin1);

            QueryWrapper qw11 =new QueryWrapper();
            qw11.eq("viewType","手机App");
            ViewStatistics phoneapp1=iViewStatisticsService.getOne(qw11);
            session.setAttribute("phoneapp1",phoneapp1);

            QueryWrapper qw12 =new QueryWrapper();
            qw12.eq("viewType","网页");
            ViewStatistics wangye1=iViewStatisticsService.getOne(qw12);
            session.setAttribute("wangye1",wangye1);

            QueryWrapper qw13 =new QueryWrapper();
            qw13.eq("viewType","微信公众号");
            ViewStatistics weixingz1=iViewStatisticsService.getOne(qw13);
            session.setAttribute("weixingz1",weixingz1);

            QueryWrapper qw14 =new QueryWrapper();
            qw14.eq("viewType","微博");
            ViewStatistics weibo1=iViewStatisticsService.getOne(qw14);
            session.setAttribute("weibo1",weibo1);

            QueryWrapper qw15 =new QueryWrapper();
            qw15.eq("viewType","头条号");
            ViewStatistics toutiao1=iViewStatisticsService.getOne(qw15);
            session.setAttribute("toutiao1",toutiao1);
            System.out.println(toutiao1.getVisitorcount());

            //获取系统消息
            QueryWrapper<SystemMessage> queryWrapper=new QueryWrapper();
            queryWrapper.orderByDesc("createtime");
            session.setAttribute("systemMessage",iSystemMessageService.list().get(0));

            path="redirect:/index";
        }
        else {
            //没有找到email
//            model.addAttribute("msg","用户名密码不正确");
            session.setAttribute("msg","用户名密码不正确");
//            path="/login";  //加/表示从项目的根目录开始找,spring boot会在templates的根下找一个login.html文件;不加表示在当前控制器找
//            path="forward:/login"; //表示请求的转发,将/customerService/login请求转发给另一个控制器去处理,找一个名为login的控制器，控制器中的请求映射
            path="redirect:/login";  //它和forward相同，不同的是它叫重定向，发送两次请求，设置的值在第一个请求里面，在到达的页面里面是取不到值的；地址栏会发生改变
        }
        return path;
    }
    @RequestMapping("/signup") //String confirmpwd,
    public String signupCloud(CustomerService customerService, Model model, HttpSession session) {
        System.out.println("12345");
//        if(confirmpwd==customerService.getPassword()){
            CustomerService user3=new CustomerService(customerService.getPassword(),customerService.getEmail(),customerService.getNickname(),customerService.getRealname(),customerService.getCustomerservicephone());
            this.iCustomerServiceService.save(user3);
            return "login";
//        } else {
//            return "signup";
//        }

    }

    @RequestMapping("/index")//@RequestParam("current") Integer current,@RequestParam("size") Integer size
    public String index(Model model){
//        IPage<CustomerService> page=new Page<>(current,size); //表
//        model.addAttribute("customerCount",iCustomerInfoService.count());
//        model.addAttribute("csCount",iCustomerServiceService.count());
        List<CustomerService> list=iCustomerServiceService.list();
//        IPage<CustomerService> page1=iCustomerServiceService.page(page);
//        List<CustomerService> list=page1.getRecords();
        //long pagesCount=page1.getPages();
        model.addAttribute("list",list);

        return "customerservice";
    }

    @RequestMapping("/update")  //@RequestParam("customerserviceid") Integer customerserviceid
    public String update(Model model, CustomerService cs){
//        System.out.println("update");
        iCustomerServiceService.getById(cs.getCustomerserviceid());

        return null;
    }
    @RequestMapping("/del")
    public String del(Model model, CustomerService cs){
//        System.out.println("del");
        iCustomerServiceService.removeById(cs.getCustomerserviceid());
        List<CustomerService> list=iCustomerServiceService.list();
        model.addAttribute("list",list);

        return "/customerservice";
    }

    @RequestMapping("/setting")
    public String setting(Model model, CustomerService cs){

        return "setting";
    }
    @RequestMapping("/settingUp")
    @ResponseBody
    public String settingUp(CustomerService customerService, HttpServletRequest request, HttpSession session){

        System.out.println(customerService);
        CustomerService cs = (CustomerService) session.getAttribute("cs");
        cs.setEmail(customerService.getEmail());
        cs.setCustomerservicephone(customerService.getCustomerservicephone());
        cs.setNickname(customerService.getNickname());
        cs.setRealname(customerService.getRealname());

        UpdateWrapper<CustomerService> updateW = new UpdateWrapper<>();
        updateW.eq("customerServiceId", cs.getCustomerserviceid());
        iCustomerServiceService.update(cs, updateW);

        session.setAttribute("cs", cs);
        System.out.println(cs);
        return "OK";
    }

    @RequestMapping("/headImg")
    @ResponseBody
    public Object uploadImage(@RequestParam("file") MultipartFile file, HttpServletRequest request, HttpSession session) {
        //获取项目classes/static的地址
        //String staticPath = getClass().getClassLoader().getResource("static").getPath();
        String fileName = file.getOriginalFilename();  //获取文件名

        // 图片存储目录及图片名称
        String url_path = "images" + File.separator + fileName;
        //String savePath = staticPath + File.separator + url_path;
        String savePath = System.getProperty("user.dir") + "/src/main/resources/static/" + url_path;
        System.out.println("图片保存地址："+savePath);
        String visitPath =url_path;
        System.out.println("图片访问uri："+visitPath);

        File saveFile = new File(savePath);
        if (!saveFile.exists()){
            saveFile.mkdirs();
        }
        try {
            file.transferTo(saveFile);  //将临时存储的文件移动到真实存储路径下
        } catch (IOException e) {
            e.printStackTrace();
        }

        CustomerService cs = (CustomerService) session.getAttribute("cs");
        cs.setHeadimg(visitPath);

        UpdateWrapper<CustomerService> updateW = new UpdateWrapper<>();
        updateW.eq("customerServiceId", cs.getCustomerserviceid());
        iCustomerServiceService.update(cs, updateW);

        session.setAttribute("cs", cs);

        return "OK";
    }




    @RequestMapping("teammanage")
    public String teammanage(Model model, HttpSession session, @RequestParam("value")String value, @RequestParam("current")Integer current, @RequestParam("size")Integer size){

        IPage<CustomerService> page =new Page<>(current,size);

        QueryWrapper<CustomerService> qw = new QueryWrapper<>();
        qw.like("state",value).or().like("email",value).or().like("nickname",value).or().like("realname",value).or().like("customerservicephone",value);
        IPage<CustomerService> page1=this.iCustomerServiceService.page(page,qw);
        List<CustomerService> list = page1.getRecords();
        session.setAttribute("notice",this.iCustomerServiceService.count());
        long pagesCount=page1.getPages();
        model.addAttribute("list",list);
        model.addAttribute("pagesCount",pagesCount);
        return "teammanage";
    }

    @RequestMapping("updaterole")
    public String updarole(Model model,CustomerService customerService){

        CustomerService c = iCustomerServiceService.getById(customerService.getCustomerserviceid());
        c.setServicerole(customerService.getServicerole());
        c.setServicegroup(customerService.getServicegroup());
        iCustomerServiceService.updateById(c);


        return "redirect:/customerService/teammanage?value=&current=1&size=6";
    }

//    @RequestMapping("updategroup")
//    public String updagroup(Model model,CustomerService customerService){
//
//        CustomerService c2 = iCustomerServiceService.getById(customerService.getCustomerserviceid());
//        c2.setServicegroup(customerService.getServicegroup());
//        iCustomerServiceService.updateById(c2);
//
//        return "redirect:/customerService/teammanage?value=&current=1&size=6";
//    }
}

